#include <iostream>
#include "ref_study.h"
#include "class_study.h"
#include "template_study.h"
#include "friend_study.h"
#include "diamond_inheritance.h"
#include <string>
#include <vector>
#include "main.h"
#include "list_node_add.h"
#include "template_special.h"
#include "string_study.h"

class Solution
{
public:
    vector<int> twoSum(vector<int> &nums, int target);
};
int main(int k, char **)
{
    cout << "Hello, from clessons!\n";

    refStudy();

    classStudy();

    friendStudy();

    templateStudy();

    diamondInheritanceStudy();

    addTwoStudy();

    addTwoNumbers();

    void test();
    test();

    fullTemplateSpecialStudy();

    stringStudy();

    
}
void fullTemplateSpecialStudy()
{
    cout << "addTwoNumbers ==================  " << endl;

    printData(10);      // 调用通用模板
    printData(3.14);    // 调用通用模板
    printData("Hello"); // 调用通用模板
    string str = "World";
    printData(str); // 调用通用模板
    const char *cstr = "C-String";
    printData(cstr); // 调用特化模板


    MyContainer<double> dc;
    dc.describe();

    MyContainer<string> sc;
    sc.describe();

    MyContainer<int> ic;
    ic.describe();
}
void addTwoNumbers()
{
    cout << "addTwoNumbers ==================  " << endl;

    ListNode *l1 = new ListNode(2, new ListNode(4, new ListNode(3)));
    ListNode *l2 = new ListNode(5, new ListNode(6, new ListNode(4)));

    Solution1 s;
    ListNode *l3 = s.addTwoNumbers(l1, l2);
    cout << "test" << endl;
}
void test()
{
    // 给定一个字符串 s ，请你找出其中不含有重复字符的 最长 子串 的长度。

    string s(" ");
    int len = s.length();
    int max = 0;
    int end = 0;
    int start = 0;
    for (int i = 0; i < len; i++)
    {
        for (int k = start; k < end; k++)
        {
            if (s[i] == s[k])
            {
                max = end - start > max ? end - start : max;
                start = k + 1;
                break;
            }
        }
        end = i + 1;
    }

    max = len - start > max ? len - start : max;
}

void addTwoStudy()
{
    cout << "addTwoStudy ==================  " << endl;

    vector<int> nums = {2, 7, 11, 15};
    Solution s;
    vector<int> r = s.twoSum(nums, 9);
    cout << r[0] << " " << r[1] << endl;
}

void diamondInheritanceStudy()
{
    cout << "diamondInheritanceStudy ==================  " << endl;

    D d(1, 2, 3, 5);
    cout << d.age << endl;

    d.printAge();
}
void templateStudy()
{
    cout << "templateStudy ==================  " << endl;

    cout << max1<int>(3, 5) << endl;

    cout << max1(3.2, 5.0) << endl;

    Tree<Integer> *tree = new Tree<Integer>();
    tree->put(new Integer(3));
    tree->put(new Integer(5));
    tree->put(new Integer(2));
    cout << tree->toString() << endl;

    Pair<int, string> p(1, "test");
    p.print();

    FixedArray<int, 5> array;

    array[4] = 8;
    cout << array.size() << endl;

    Wrapper<vector, int> wapper;

    wapper.add(1);
    wapper.add(2);

    wapper.add(3);
    wapper.print();
    Tuple<> t1;
    Tuple<int> t2(1);
    cout << t2.get<0>() << endl;

    Tuple<int, string> t3(1, "test");
    cout << t3.get<0>() << endl;
    cout << t3.get<1>() << endl;

    ConfigurableContainer contain(3.0);
    ConfigurableContainer<string> contain1(3.0);

    cout << "testxx" << endl;

    SimpleMap<string, int> map;
    map.insert("key1", 1);
    // map.insert("key2","2"); //error
    map.insert("key2", 2);
    cout << *map.find("key2") << endl;
}

void friendStudy()
{

    cout << "friendStudy ==================  " << endl;

    Base base(20);

    Friend f;

    f.readBase(base);

    printBaseData(base);
    Friend1 f1;
    f1.printBaseData(base);
}

void classStudy()
{
    cout << "classStudy ==================  " << endl;

    // 类示例
    Bird bird(20);

    Animal &animal = bird;

    animal.eat();

    bird.fly();

    Animal *an = new Bird(20);

    an->eat();

    delete an;
}

void refStudy()
{
    cout << "refStudy ==================  " << endl;

    int a = 5, b = 6;

    // 引用绑定函数入口参数
    swap(a, b);
    // swap(5,7); error
    cout << a << " : " << b << endl;

    // 返回值是引用
    int &t = retRef();
    cout << " t= " << t << endl;
    t = 7;
    cout << " t= " << t << endl;

    // 数组的引用
    Array arr(20);
    arr[2] = 3;
    // arr[20] = 3; error

    vector<string> vec = {"hello", "world", "!"};

    for (const auto &str : vec)
    {
        cout << str << " ";
    }

    for (auto &str : vec)
    {
        str += " modified";
    }

    for (const auto &str : vec)
    {
        cout << str << " ";
    }

    // 左值和右值的区别

    int a1 = 10;  // a是左值
    int *p = &a1; //  可以取地址

    string s;    // s是左值
    s = "hello"; // 可以出现在赋值的左边

    int arr1[10]; // arr是左值

    int b1 = 20; // 20是右值
    string f1(); // 函数声明
    // string s = f1(); //f()的返回值是右值

    int c = a + b; // (a + b) 的结果是右值

    string s2 = s + " world!"; // 拼接的结果是右值

    int x = 10;
    int &ref1 = x;
    // int &ref2 = 20; error

    const int &ref3 = 30;
    // int &&ref3 =30;

    int y = 10;
    // int &&ref4=x; error

    int &&ref4 = 20;
    // string &&ref5 = f1();
    int &&ref6 = y + 20;
    int &&ref7 = y + ref4;

    // String sx = createStr();
    String sx("test");
    String sx1 = sx;
    String sx2 = move(sx);
    string &&r = string("tx");

    // String sx[] = {"test"};
    // String sx1 = sx[0];
    // String sx2 = move(sx[0]);

    // String sy(sx);
    // String sz(move(sx));
    // String l = create<String>(sx);
    // String l1 = create<String>(move(sx));
    int *ptr = new int;

    int &r = *ptr;

    r=5;
    cout << "*ptr:" << *ptr << endl;

    cout << "ok" << endl;
}
